package com.zoe.wan.android.example.fragment.hot_key


import android.content.Intent
import android.os.Bundle
import com.zoe.wan.base.BaseFragment
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.activity.search.SearchActivity
import com.zoe.wan.android.example.activity.webView.WebActivity
import com.zoe.wan.android.example.common.adapter.CommonSearchGridAdapter
import com.zoe.wan.android.example.databinding.FragmentSearchKeyBinding
import com.zoe.wan.android.example.repository.data.CommonSearchData
import com.zoe.wan.base.adapter.BaseItemClickListener
import com.zoe.wan.base.view.NoScrollLayoutManager

class FragSearchKey : BaseFragment<FragmentSearchKeyBinding, FragSearchKeyViewModel>() {

    private val searchListAdapter = CommonSearchGridAdapter()
    private val commonWebsiteAdapter = CommonSearchGridAdapter()

    override fun getLayoutId(): Int {
        return R.layout.fragment_search_key
    }

    override fun getViewModelId(): Int {
        return BR.searchKeyVm
    }

    override fun initViewData() {
        initListView()

        viewModel?.searchKeyList?.observe(viewLifecycleOwner) {
            searchListAdapter.setDataList(it)
        }

        viewModel?.commonWebsiteList?.observe(viewLifecycleOwner) {
            commonWebsiteAdapter.setDataList(it)
        }
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        savedInstanceState?.let {
            val searchList = it.getParcelableArrayList<CommonSearchData>("searchList")
            val websiteList = it.getParcelableArrayList<CommonSearchData>("commonWebsiteList")
            searchListAdapter.setDataList(searchList)
            commonWebsiteAdapter.setDataList(websiteList)
        }
    }


    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putParcelableArrayList("searchList", ArrayList(viewModel?.searchKeyList?.value))
        outState.putParcelableArrayList("commonWebsiteList", ArrayList(viewModel?.commonWebsiteList?.value))
    }

    private fun initListView() {
        // 搜索热点
        binding?.searchHotKeyListView?.layoutManager = NoScrollLayoutManager(context, 3)
        binding?.searchHotKeyListView?.adapter = searchListAdapter

        searchListAdapter.registerItemClickListener(object : BaseItemClickListener<CommonSearchData?>() {
            override fun itemClick(item: CommonSearchData?, position: Int) {
                val intent = Intent(context, SearchActivity::class.java)
                intent.putExtra(SearchActivity.INTENT_KEYWORD, item?.name)
                startActivity(intent)
            }
        })

        // 常用网站
        binding?.commonWebsiteListView?.layoutManager = NoScrollLayoutManager(context, 3)
        binding?.commonWebsiteListView?.adapter = commonWebsiteAdapter

        commonWebsiteAdapter.registerItemClickListener(object : BaseItemClickListener<CommonSearchData?>() {
            override fun itemClick(item: CommonSearchData?, position: Int) {
                jumpToWeb(item?.name, item?.link)
            }
        })
    }

    private fun jumpToWeb(title: String?, url: String?) {
        val intent = Intent(context, WebActivity::class.java)
        intent.putExtra(WebActivity.INTENT_WEB_TITLE_KEY, title)
        intent.putExtra(WebActivity.INTENT_WEB_URL_KEY, url)
        startActivity(intent)
    }
}